package com.sskj.one.mine;

import android.Manifest;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.alibaba.android.arouter.launcher.ARouter;
import com.hjq.toast.ToastUtils;
import com.luck.picture.lib.permissions.RxPermissions;
import com.sskj.common.adapter.slimadapter.IViewInjector;
import com.sskj.common.adapter.slimadapter.SlimAdapter;
import com.sskj.common.adapter.slimadapter.SlimInjector;
import com.sskj.common.base.App;
import com.sskj.common.rxbus2.RxBus;
import com.sskj.common.util.ClickUtil;
import com.sskj.common.util.ImageUtil;
import com.sskj.common.util.NumberUtil;
import com.sskj.lib.base.BaseActivity;
import com.sskj.lib.router.provider.LogoutProvider;
import com.sskj.lib.util.CopyUtil;
import com.sskj.one.R;
import com.sskj.one.bean.InviteBean;
import com.sskj.one.bean.SevenBean;
import com.sskj.one.bean.bus.TabBean;
import com.sskj.one.util.PictureUtil;
import com.sskj.one.view.TipDialog;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import io.reactivex.Observable;

/**
 * @author Hey
 * Create at  2019/05/22
 */
public class InviteActivity extends BaseActivity<InvitePresenter> {


    @BindView(R.id.iv_qrcode)
    ImageView ivQrcode;
    @BindView(R.id.tv_code)
    TextView tvCode;
    @BindView(R.id.tv_url)
    TextView tvUrl;
    @BindView(R.id.iv_copy_code)
    ImageView ivCopyCode;
    @BindView(R.id.iv_copy_url)
    ImageView ivCopyUrl;
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.tvTitle)
    TextView tvTitle;
    @BindView(R.id.tvRight)
    TextView tvRight;
    @BindView(R.id.ivBack)
    LinearLayout ivBack;

    private SlimAdapter<levelBean> slimAdapter;
    private String qcrode;

    @Override
    public int getLayoutId() {
        return R.layout.app_activity_invite;
    }

    @Override
    public InvitePresenter getPresenter() {
        return new InvitePresenter();
    }

    @Override
    public void initView() {
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        slimAdapter = SlimAdapter.create().register(R.layout.item_invite, new SlimInjector<levelBean>() {
            @Override
            public void onInject(levelBean data, IViewInjector iViewInjector, List list) {
                iViewInjector.text(R.id.tv_level, data.getLevel())
                        .text(R.id.tv_rate, "用户交易金额*" + NumberUtil.keep(data.getRate(), 3) + "%");
            }
        }).attachTo(recyclerView).updateData(new ArrayList());

    }

    @Override
    public void initData() {
        mPresenter.getInvite();
        mPresenter.getSevenLevel();
        ClickUtil.click(300, ivCopyCode, () -> {
            if (isEmpty(tvCode)) {
                return;
            }
            CopyUtil.copy(getText(tvCode));
        });
        ClickUtil.click(300, ivCopyUrl, () -> {
            if (isEmpty(tvUrl)) {
                return;
            }
            CopyUtil.copy(getText(tvUrl));
        });
        ClickUtil.click(300, tvRight, () -> {
            RxBus.getDefault().post(new TabBean(1));
            finish();
        });
        ivQrcode.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                if (isEmpty(qcrode)) {
                    ToastUtils.show("获取二维码信息失败");
                    return false;
                }
                new TipDialog(InviteActivity.this)
                        .setContentText("保存二维码到相册")
                        .setCancel(true)
                        .setListener(dialog -> {
                            new RxPermissions(InviteActivity.this).request(Manifest.permission.WRITE_EXTERNAL_STORAGE)
                                    .subscribe(aBoolean -> {
                                        if (aBoolean) {
                                            if (PictureUtil.saveImageToGallery(InviteActivity.this, ivQrcode, "VaPay_recharge.jpg")) {
                                                ToastUtils.show("保存成功");
                                            } else {
                                                ToastUtils.show("保存失败");
                                            }
                                        } else {
                                            ToastUtils.show("保存失败，请先开启文件权限");
                                        }
                                    });
                        }).show();
                return false;
            }
        });
//        ClickUtil.click(300, ivQrcode, () -> {
//            if (isEmpty(qcrode)) {
//                ToastUtils.show("获取二维码信息失败");
//                return;
//            }
//            new TipDialog(this)
//                    .setContentText("保存二维码到相册")
//                    .setCancel(true)
//                    .setListener(dialog -> {
//                        new RxPermissions(this).request(Manifest.permission.WRITE_EXTERNAL_STORAGE)
//                                .subscribe(aBoolean -> {
//                                    if (aBoolean) {
//                                        if (PictureUtil.saveImageToGallery(this, ivQrcode, "VaPay_recharge.jpg")) {
//                                            ToastUtils.show("保存成功");
//                                        } else {
//                                            ToastUtils.show("保存失败");
//                                        }
//                                    } else {
//                                        ToastUtils.show("保存失败，请先开启文件权限");
//                                    }
//                                });
//                    }).show();
//        });

    }

    public static void start(Context context) {
        Intent intent = new Intent(context, InviteActivity.class);
        context.startActivity(intent);
    }

    public void getInviteOK(InviteBean data) {
        if (data == null) {
            return;
        }
        this.qcrode = data.getQrc();
        ImageUtil.setImage(data.getQrc(), ivQrcode);
        tvUrl.setText(data.getUrl());
        tvCode.setText(data.getAccount());
    }

    public void getSevenOK(SevenBean data) {
        if (data == null) {
            return;
        }
        List<levelBean> levelBeans = new ArrayList<>();
        levelBeans.add(new levelBean("一级", data.getCommission_ratio1()));
        levelBeans.add(new levelBean("二级", data.getCommission_ratio2()));
        levelBeans.add(new levelBean("三级", data.getCommission_ratio3()));
        levelBeans.add(new levelBean("四级", data.getCommission_ratio4()));
        levelBeans.add(new levelBean("五级", data.getCommission_ratio5()));
        levelBeans.add(new levelBean("六级", data.getCommission_ratio6()));
        levelBeans.add(new levelBean("七级", data.getCommission_ratio7()));
        if (slimAdapter != null) {
            slimAdapter.updateData(levelBeans);
        }

    }

    private class levelBean {
        private String level;
        private String rate;

        public levelBean(String level, String rate) {
            this.level = level;
            this.rate = rate;
        }

        public String getLevel() {
            return level == null ? "" : level;
        }

        public void setLevel(String level) {
            this.level = level;
        }

        public String getRate() {
            return rate == null ? "" : rate;
        }

        public void setRate(String rate) {
            this.rate = rate;
        }
    }
}
